using System.Collections;

using LocalBoard.Domain;
using LocalBoard.Persistence;

namespace LocalBoard.Controller
{
	[NUnit.Framework.TestFixture]
	public class TestMessageController
	{
		[NUnit.Framework.SetUp]
		public void setUp()
		{
			_accessorFactory = new MockAccessorFactory();
			_user = _accessorFactory.GetProfileAccessor().Get(_accessorFactory.GetProfileAccessor().Insert(new Profile("userId 1","userName 1","password","test@test.com")));
			int clubId = _accessorFactory.GetClubAccessor().Insert(new Club("testClub", "test club", 1, "","", null));
			ClubController clubController = new ClubController(clubId, _accessorFactory);
			int boardId = clubController.CreateBoard(new Board("test board", null, 0, 0, clubController.ClubId, BoardViewType.List));
			BoardController boardController = new BoardController(boardId,clubController, 1, _accessorFactory); 
			_messageId = boardController.PostMessage("subject 1","body 1",_user,0);    
			_unitUnderTest = new MessageController(_messageId, _accessorFactory); 
		}
		[NUnit.Framework.TearDown]
		public void tearDown()
		{
			MockAccessorFactory.ResetDataTable();

			_unitUnderTest = null;
		}
		[NUnit.Framework.Test]
		public void testPostComment()
		{
			_unitUnderTest.PostComment("test comment", _user);

			Message message = _accessorFactory.GetMessageAccessor().Get(_messageId);
			NUnit.Framework.Assertion.AssertNotNull(message);
			foreach(Comment comment in message.Comments)
			{
				if(comment.Body == "test comment")
				{
					NUnit.Framework.Assertion.Assert(true);
					return;
				}
			}
			NUnit.Framework.Assertion.Fail("Can't find posted comment");
		}
		[NUnit.Framework.Test]
		public void testDeleteComment()
		{
			int commentId = _unitUnderTest.PostComment("test comment2", _user);
			_unitUnderTest.DeleteComment(commentId);
             
			Message message = _accessorFactory.GetMessageAccessor().Get(_messageId);
			foreach(Comment comment in message.Comments)
			{
				if(comment.Body == "test comment2")
				{
					NUnit.Framework.Assertion.Fail("Can't delete posted comment");
					return;
				}
			}
			NUnit.Framework.Assertion.Assert(true);
		}

		private int _messageId;
		private Profile _user = null;
		private MockAccessorFactory _accessorFactory = null;
		private MessageController _unitUnderTest;
	}
}
